//
//  JBBasicDataCollectionViewModuleController.m
//  JustBen
//
//  Created by Yongbin Zhang on 12/4/13.
//  Copyright (c) 2013 ZhangYongbin(JustBen). All rights reserved.
//

#import "JBBasicDataCollectionViewModuleController.h"

static NSInteger const kDefaultLoadCount = 12;



@interface JBBasicDataCollectionViewModuleController () <UICollectionViewDataSource, UICollectionViewDelegate>


@end



@implementation JBBasicDataCollectionViewModuleController

#pragma mark - init
- (id)init
{
    self = [super init];
    if (self) {
        _flowLayoutSectionInset = UIEdgeInsetsMake(5.0f, 10.0f, 5.0f, 10.0f);
        
        _cellClassName = NSStringFromClass([JBBasicDataCollectionViewCell class]);
        
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        [flowLayout setItemSize:CGSizeMake([JBBasicDataCollectionViewCell cellWidth], [JBBasicDataCollectionViewCell cellHeight])];
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        flowLayout.sectionInset = _flowLayoutSectionInset;
        
        _collectionView = [[JBBasicDataCollectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.width, 0.0f) collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor clearColor];
    }
    
    return self;
}


#pragma mark - setters

- (void)setCellNibName:(NSString *)cellNibName
{
    if (cellNibName) {
        if (![cellNibName isEqualToString:_cellNibName]) {
            _cellNibName = [cellNibName copy];
            
            _cellClassName = [cellNibName copy];
        }
    }
}


#pragma mark - object methods

//  模版方法
//  重新加载数据
- (void)reloadData
{
    [self reloadDataWithSuccess:nil failure:nil];
}

- (void)reloadDataWithSuccess:(void (^)())success failure:(void (^)(NSInteger errorCode, NSString *errorMsg))failure
{
    [super reloadDataWithSuccess:success failure:failure];
}

- (void)reloadDataSuccessedWithLastPos:(NSString *)lastPos dataList:(NSArray *)dataList
{
    [super reloadDataSuccessedWithLastPos:lastPos dataList:dataList];
    
    [self.collectionView reloadData];
}

- (void)reloadDataFailed
{
    [super reloadDataFailed];
    
    [self.collectionView reloadData];
}


//  模版方法
//  加载更多的数据
- (void)loadMoreData
{
    [self loadMoreDataWithSuccess:nil failure:nil];
}

- (void)loadMoreDataWithSuccess:(void (^)())success failure:(void (^)(NSInteger errorCode, NSString *errorMsg))failure
{
    [super loadMoreDataWithSuccess:success failure:failure];
}

- (void)loadMoreDataSuccessedWithLastPos:(NSString *)lastPos dataList:(NSArray *)dataList
{
    [super loadMoreDataSuccessedWithLastPos:lastPos dataList:dataList];
    
    if (dataList && dataList.count > 0) {
        [self.collectionView reloadData];
    }
}

- (void)loadMoreDataFailed
{
    [super loadMoreDataFailed];
}


//  模版方法
//  设置CollectionViewFlowLayout
- (void)setCollectionViewFlowLayout
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    Class cellClass = NSClassFromString(self.cellClassName);
    if (cellClass) {
        [flowLayout setItemSize:CGSizeMake([cellClass cellWidth], [cellClass cellHeight])];
    } else {
        [flowLayout setItemSize:CGSizeMake(90.0f, 90.0f)];
    }
    
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    flowLayout.sectionInset = self.flowLayoutSectionInset;
    
    [self.collectionView setCollectionViewLayout:flowLayout];
}


//  模版方法
//  选中某一行，以该行对应的数据为参数
- (void)selectedCellAtIndexPath:(NSIndexPath *)indexPath
{
    
}


#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifer;
    Class cellClass;
    
    if (self.cellNibName && self.cellNibName.length > 0) {
        cellIdentifer = self.cellNibName;
    } else if (self.cellClassName && self.cellClassName.length > 0) {
        cellIdentifer = self.cellClassName;
        cellClass = NSClassFromString(self.cellClassName);
    } else {
        cellIdentifer = NSStringFromClass([JBBasicDataCollectionViewCell class]);
        cellClass = [JBBasicDataCollectionViewCell class];
    }
    
    
    JBBasicDataCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifer forIndexPath:indexPath];
    
    if (!cell) {
        if (self.cellNibName) {
            cell = [[[NSBundle mainBundle] loadNibNamed:self.cellNibName owner:self options:nil] lastObject];
        } else {
            cell = [[cellClass alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [cellClass cellWidth], [cellClass cellHeight])];
        }
    }
    
    cell.cellData = [self.dataList objectAtIndex:indexPath.row];
    
    return cell;
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self selectedCellAtIndexPath:indexPath];
}


#pragma mark - scrollView delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.shouldReload) {
        //  TO DO
    }
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (self.shouldReload) {
        //  TO DO
    }
    
    CGFloat contentOffsetY = scrollView.contentOffset.y;
    CGFloat contentHeight = scrollView.contentSize.height;
    CGFloat height = scrollView.bounds.size.height;
    
    if (contentOffsetY + height >= contentHeight) {
        if (self.shouldLoadMore) {
            [self loadMoreData];
        }
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat contentOffsetY = scrollView.contentOffset.y;
    CGFloat contentHeight = scrollView.contentSize.height;
    CGFloat height = scrollView.bounds.size.height;
    
    if (contentOffsetY + height >= contentHeight) {
        if (self.shouldLoadMore) {
            [self loadMoreData];
        }
    }
}


@end
